package myDemo.bobo.game.wuziqiAiMy.demo;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import myDemo.bobo.game.wuziqiAiMy.BaseComputerAi;
import myDemo.bobo.game.wuziqiAiMy.HumanPlayer;
import myDemo.bobo.game.wuziqiAiMy.IChessboard;
import myDemo.bobo.game.wuziqiAiMy.Point;

public class RootFrame extends JPanel implements MouseListener {
	Color colortemp;
	private static IChessboard iboard;
	private HumanPlayer HM;
	private BaseComputerAi AI;
	final int MARGIN;
	final int GRID_SPAN;
	final int ROWS;
	final int COLS;
	final Image img;
	int xIndex, yIndex;

	public RootFrame() {
		iboard = new ChessBoardImpl();
		HM = new HumanPlayer();
		AI = new BaseComputerAi();
		HM.setChessboard(iboard);
		AI.setChessboard(iboard);
		MARGIN = iboard.getMargin();
		GRID_SPAN = iboard.getGridSpan();
		ROWS = iboard.getMaxY();
		COLS = iboard.getMaxX();
		img = iboard.getImg();
		addMouseListener(this);
		addMouseMotionListener(new MouseMotionListener() {
			public void mouseDragged(MouseEvent e) {
			}

			public void mouseMoved(MouseEvent e) {
				int x1 = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
				// 将鼠标点击的坐标位置转成网格索引
				int y1 = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
				// 游戏已经结束不能下
				// 落在棋盘外不能下
				// x，y位置已经有棋子存在，不能下
				if (hasChess(x1, y1)) {
					setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
				} else {
					// 设置成默认状态
					setCursor(new Cursor(Cursor.HAND_CURSOR));
				}
			}
		});
	}

	private boolean hasChess(int x, int y) {
		List<Point> freePoints = iboard.getReadyPlayPoints();
		for (Point p : freePoints) {
			if (p.getX() == x && p.getY() == y) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// 游戏结束时，不再能下
		if (iboard.isGameOver())
			return;
		boolean isBlack = iboard.isBlackFirst();
		// 将鼠标点击的坐标位置转换成网格索引
		xIndex = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
		yIndex = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
		if (xIndex < 0 || xIndex > ROWS || yIndex < 0 || yIndex > COLS)
			return;
		// 落在棋盘外不能下
		Point ch = new Point(xIndex, yIndex, isBlack ? Color.black : Color.white);
		// 如果x，y位置已经有棋子存在，不能下
		if (hasChess(xIndex, yIndex))
			return;
		HM.run(null, ch);
		iboard.removePoint(ch);
		// 可以进行时的处理
		repaint();// 通知系统重新绘制
		// 如果胜出则给出提示信息，不能继续下棋
		iboard.setBlack(!isBlack);
		if (HM.hasWin()) {
			String msg = String.format("恭喜，%s赢了！", "黑棋");
			JOptionPane.showMessageDialog(this, msg);
			iboard.setGameOver();
		} else {
			Point p = AI.run(HM.getMyPoints(), null);
			p.setColor(iboard.isBlackFirst() ? Color.black : Color.white);
			iboard.removePoint(p);
			iboard.setBlack(!iboard.isBlackFirst());
			if (AI.hasWin()) {
				String msg = String.format("恭喜，%s赢了！", "白棋");
				JOptionPane.showMessageDialog(this, msg);
			}
			repaint();// 通知系统重新绘制
		}

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	// 绘制
	public void paintComponent(Graphics g) {
		List<Point> readyPoints = iboard.getReadyPlayPoints();
		super.paintComponent(g);// 画棋盘
		int imgWidth = img.getWidth(this);
		int imgHeight = img.getHeight(this);// 获得图片的宽度与高度
		int FWidth = getWidth();
		int FHeight = getHeight();// 获得窗口的宽度与高度
		int x = (FWidth - imgWidth) / 2;
		int y = (FHeight - imgHeight) / 2;
		g.drawImage(img, x, y, null);
		for (int i = 0; i <= ROWS; i++) {// 画横线
			g.drawLine(MARGIN, MARGIN + i * GRID_SPAN, MARGIN + COLS * GRID_SPAN, MARGIN + i * GRID_SPAN);
		}
		for (int i = 0; i <= COLS; i++) {// 画竖线
			g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN, MARGIN + ROWS * GRID_SPAN);
		}
		// 画棋子
		for (int i = 0; i < readyPoints.size(); i++) {
			Point tp = readyPoints.get(i);
			// 网格交叉点x，y坐标
			int xPos = tp.getX() * GRID_SPAN + MARGIN;
			int yPos = tp.getY() * GRID_SPAN + MARGIN;
			g.setColor(tp.getColor());// 设置颜色
			// g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2,
			// Point.DIAMETER, Point.DIAMETER);
			// g.drawImage(shadows, xPos-Point.DIAMETER/2,
			// yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER, null);
			colortemp = tp.getColor();
			if (colortemp == Color.black) {
				RadialGradientPaint paint = new RadialGradientPaint(xPos - tp.getDiameter() / 2 + 25,
						yPos - tp.getDiameter() / 2 + 10, 20, new float[] { 0f, 1f },
						new Color[] { Color.WHITE, Color.BLACK });
				((Graphics2D) g).setPaint(paint);
				((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
						RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);

			} else if (colortemp == Color.white) {
				RadialGradientPaint paint = new RadialGradientPaint(xPos - tp.getDiameter() / 2 + 25,
						yPos - tp.getDiameter() / 2 + 10, 70, new float[] { 0f, 1f },
						new Color[] { Color.WHITE, Color.BLACK });
				((Graphics2D) g).setPaint(paint);
				((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
						RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);

			}

			Ellipse2D e = new Ellipse2D.Float(xPos - tp.getDiameter() / 2, yPos - tp.getDiameter() / 2, 34, 35);
			((Graphics2D) g).fill(e);
			// 标记最后一个棋子的红矩形框

			if (i == readyPoints.size()) {// 如果是最后一个棋子
				g.setColor(Color.red);
				g.drawRect(xPos - tp.getDiameter() / 2, yPos - tp.getDiameter() / 2, 34, 35);
			}
		}
	}

	public void restartGame() {
		iboard.reset();
		HM = new HumanPlayer();
		AI = new BaseComputerAi();
		HM.setChessboard(iboard);
		AI.setChessboard(iboard);
		repaint();
	}

	// 悔棋
	public void goback() {
		//repaint();
	}
	// 矩形Dimension

	public Dimension getPreferredSize() {
		return new Dimension(MARGIN * 2 + GRID_SPAN * COLS, MARGIN * 2 + GRID_SPAN * ROWS);
	}
}
